*****************************************************
*****************dataanalysis dofile*****************
*****************************************************

clear all
clear matrix
set more off, perm
cd "D:/Data/ProjData/Organizations, political participation/mod/"
do "C:/Userdata/Shared/Dofiles/DoData/Organizations, political participation/programs.do"

capture log close
log using "C:/Userdata/Shared/Logs/Organizations, political participation/dataanalysis.text", replace text 

********************************************************************************
// Macros
********************************************************************************

* Variable macros
global cov 		"yearseducation socialbidragfamilj sysselsatt age gender immig zLoneInk"
global covpanel "yearseducation socialbidragfamilj sysselsatt age zLoneInk"
global covpanelsplit "yearseducation socialbidragfamilj sysselsatt zLoneInk"

* Output macros
#delimit ;
global textable		se nonotes nogaps star(* 0.10 ** 0.05 *** 0.01) label replace b(3) booktabs alignment(D{.}{.}{-1}) compress mlabels() eqlabels(none);
global robustnote	addnote("Robust standard errors in parenthesis. * p<0.1, ** p<0.05, *** p<0.01");
global robustainote	addnote("Robust Abadie--Imbens standard errors in parenthesis. * p<0.1, ** p<0.05, *** p<0.01");
global clusternote	addnote("Clustered robust standard errors on the individual level in parenthesis." "* p<0.1, ** p<0.05, *** p<0.01");
global stats1		stats(icovs data r2_a N, 
						label("Covariates?" "Time dimension data?" "Adjusted R2" "Observations") 
						fmt(0 0 3 0 %11.0fc) 
						layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")
					);
global stats2		stats(icovs ifix yfix data r2_a N, 
						label("Covariates?" "Individual fixed effects?" "Time period fixed effects?" "Time dimension data?" "Adjusted R2" "Observations") 
						fmt(0 0 0 0 3 0 %11.0fc) 
						layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")
					);
global stats3		stats(sample N, 
						label("Sample" "Observations") 
						fmt(0 %11.0fc) 
						layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")
					);					
global bothtitles 	mtitles("Turnout EP" "Turnout EP" "PP factor score" "PP factor score");
global bothtitles6c	mtitles("Turnout EP" "Turnout EP" "Turnout EP" "PP factor score" "PP factor score" "PP factor score");
global indextitle 	mtitles("Index pol.part" "Index pol.part");
global eptitles		mtitles("Turnout EP" "Turnout EP" "Turnout EP" "Turnout EP");
global factortitles mtitles("PP factor score" "PP factor score" "PP factor score" "PP factor score");	
global separatetitle mtitles("Con Muni.of" "L.Comment" "L.appeal" "L.Demon" "L.Other" "Comment" "Appeal" "Demon");	
#delimit cr


********************************************************************************
// Analyses presented in main text
********************************************************************************

* Table 1: With covariates*
eststo clear
use "dataanalysis_turnout", clear
eststo: reg turnout_eu memberany activeany $cov, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
eststo: reg turnout_eu Nmemberships Nactive $cov, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
use "dataanalysis_otherparticipation", clear
eststo: reg factor memberany activeany $cov, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
eststo: reg factor Nmemberships Nactive $cov, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table1.tex", $textable $stats1 $bothtitles $robustnote keep(_cons memberany activeany Nmemberships Nactive)
eststo clear


* Table 2: Panel analysis * 
use "dataanalysis_turnout", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period
eststo: xtreg turnout_eu memberany activeany y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local data "Panel"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
eststo: xtreg turnout_eu Nmemberships Nactive y2 $covpanel, fe cluster(LopNr)
	estadd local icovs "Yes"
	estadd local data "Panel"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period
eststo: xtreg factor memberany activeany y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local data "Panel"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
eststo: xtreg factor Nmemberships Nactive y2 $covpanel, fe cluster(LopNr)
	estadd local icovs "Yes"
	estadd local data "Panel"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table2.tex", $textable $stats2 $bothtitles $clusternote keep(_cons memberany activeany Nmemberships Nactive)
eststo clear


* Table 3: Panel analysis with different organizations grouped together ***
use "dataanalysis_turnout", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period
foreach k in member active {
	gen `k'anyhigh = 0
	replace `k'anyhigh= 1 if  `k'pension == 1 | `k'peace == 1  
	replace `k'anyhigh = . if `k'pension == . | `k'peace == . & `k'boende == . & `k'sport == . & `k'culture == . 
}
foreach k in member active {
	gen `k'anylow = 0
	replace `k'anylow= 1 if `k'boende == 1 | `k'sport == 1 | `k'culture == 1   
	replace `k'anylow = . if `k'boende == . | `k'sport == . | `k'culture == . & `k'pension == . & `k'peace == . 
}
egen Nmemberhigh = rowtotal(memberpension memberpeace) 
egen Nactivehigh = rowtotal(activepension activepeace) 
egen Nmemberlow = rowtotal(memberboende membersport memberculture)
egen Nactivelow = rowtotal(activeboende activesport activeculture)
label var memberanyhigh "Member in any high-selection"
label var memberanylow "Member in any low-selection" 
label var activeanyhigh "Active in any high-selection"
label var activeanylow "Active in any low-selection" 
label var Nmemberhigh "Member in \# of high-selection" 
label var Nmemberlow "Member in \# of low-selection"  
label var Nactivehigh "Active in \# of high-selection" 
label var Nactivelow "Active in \# of low-selection" 

eststo: xtreg turnout_eu memberanyhigh activeanyhigh memberanylow activeanylow y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
eststo: xtreg turnout_eu Nmemberhigh Nactivehigh Nmemberlow Nactivelow y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
	
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period	
egen Nmemberhigh = rowtotal(memberpension memberenviron memberparent memberwoman) 
egen Nactivehigh = rowtotal(activepension activeenviron activeparent activewoman) 
egen Nmemberlow = rowtotal(memberboende membersport memberhandi memberimmig)
egen Nactivelow = rowtotal(activeboende activesport activehandi activeimmig)

foreach k in member active {
	gen `k'anyhigh = 0
	replace `k'anyhigh= 1 if `k'pension == 1 | `k'environ == 1 | `k'parent == 1 | `k'woman == 1   
	replace `k'anyhigh = . if `k'pension == . | `k'environ == . | `k'parent == 1 |`k'woman == 1 & `k'boende == . & `k'sport == . & `k'handi == . & `k'immig == .  
}

foreach k in member active {
	gen `k'anylow = 0
	replace `k'anylow= 1 if `k'boende == 1 | `k'sport == 1 | `k'handi == 1 | `k'immig == 1   
	replace `k'anylow = . if `k'boende == 1 | `k'sport == 1 | `k'handi == 1 | `k'immig == 1 & `k'pension == . & `k'environ == . & `k'parent == . & `k'woman == .  
}

label var memberanyhigh "Member in any high-selection"
label var memberanylow "Member in any low-selection" 
label var activeanyhigh "Active in any high-selection"
label var activeanylow "Active in any low-selection" 
label var Nmemberhigh "Member in \# of high-selection" 
label var Nmemberlow "Member in \# of low-selection"  
label var Nactivehigh "Active in \# of high-selection" 
label var Nactivelow "Active in \# of low-selection" 

eststo: xtreg factor memberanyhigh activeanyhigh memberanylow activeanylow y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
eststo: xtreg factor Nmemberhigh Nactivehigh Nmemberlow Nactivelow y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table3.tex", $textable $stats2 $bothtitles $clusternote keep(_cons Nmember* Nactive* memberany* activeany*)
eststo clear	



********************************************************************************
// Analyses presented in the online appendix
********************************************************************************

*Unconditional regressions*	
use "dataanalysis_turnout", clear
eststo clear
eststo: reg turnout_eu memberany activeany, robust 
estadd local icovs "No"
estadd local data "Pooled data"

eststo: reg turnout_eu Nmemberships Nactive, robust 
estadd local icovs "No"
estadd local data "Pooled data"

use "dataanalysis_otherparticipation", clear
eststo: reg factor memberany activeany, robust 
estadd local icovs "No"
estadd local data "Pooled data"

eststo: reg factor Nmemberships Nactive, robust 
estadd local icovs "No"
estadd local data "Pooled data"

esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table1_raw.tex", $textable $stats1 $bothtitles $robustnote
eststo clear


*Descriptives: Assessing the variance: Variance decomposition*
/*Turnout EU*/
use "dataanalysis_turnout", clear
destring LopNr period, replace
xtset LopNr period
xtsum turnout_eu 
matrix mean_turnout = r(mean)
matrix sdT = r(sd)
matrix sdB = r(sd_b)
matrix sdW = r(sd_w)
matrix obs = r(N)
matrix regmat = [mean_turnout[1,1] \ sdT[1,1] \ sdB[1,1] \ sdW[1,1] \ obs[1,1]]
esttab matrix(regmat) using "C:/Userdata/Shared/Output/Organizations, political participation/VD_EP.tex", coeflabels(r1 "Mean dependent variable" r2 "SD overall" r3 "SD between" r4 "SD within" r5 "N.") collabels("Turnout EP") nomtitle replace

/*Factor*/
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
xtset LopNr period
xtsum factor 
matrix mean_other = r(mean)
matrix sdT = r(sd)
matrix sdB = r(sd_b)
matrix sdW = r(sd_w)
matrix obs = r(N)
matrix regmat = [mean_other[1,1] \ sdT[1,1] \ sdB[1,1] \ sdW[1,1] \ obs[1,1]]
esttab matrix(regmat) using "C:/Userdata/Shared/Output/Organizations, political participation/VD_other.tex", coeflabels(r1 "Mean dependent variable" r2 "SD overall" r3 "SD between" r4 "SD within" r5 "N.") collabels("Factor score") nomtitle replace

/*Number of observations*/
use "dataanalysis_turnout", clear
destring LopNr period, replace
xtset LopNr period
xtsum Nmemberships 
matrix mean_Nmemberships = r(mean)
matrix sdT = r(sd)
matrix sdB = r(sd_b)
matrix sdW = r(sd_w)
matrix obs = r(N)
matrix regmat = [mean_turnout[1,1] \ sdT[1,1] \ sdB[1,1] \ sdW[1,1] \ obs[1,1]]
esttab matrix(regmat) using "C:/Userdata/Shared/Output/Organizations, political participation/VD_NMem_turnout.tex", coeflabels(r1 "Mean dependent variable" r2 "SD overall" r3 "SD between" r4 "SD within" r5 "N.") collabels("Number of memberships") nomtitle replace

/*Number of observations*/
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
xtset LopNr period
xtsum Nmemberships 
matrix mean_Nmemberships = r(mean)
matrix sdT = r(sd)
matrix sdB = r(sd_b)
matrix sdW = r(sd_w)
matrix obs = r(N)
matrix regmat = [mean_turnout[1,1] \ sdT[1,1] \ sdB[1,1] \ sdW[1,1] \ obs[1,1]]
esttab matrix(regmat) using "C:/Userdata/Shared/Output/Organizations, political participation/VD_NMem_other.tex", coeflabels(r1 "Mean dependent variable" r2 "SD overall" r3 "SD between" r4 "SD within" r5 "N.") collabels("Number of memberships") nomtitle replace




* Factor loadings and screeplot *
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period


drop factor
ds rp_*
factor `r(varlist)'
screeplot, graphregion(fcolor(white)color(white) lwidth(large)) yla(,nogrid) title("") xla(1(1)8)
graph export "C:/Userdata/Shared/Output/Organizations, political participation/screeplot.pdf", as(pdf) replace
ds rp_*
factor `r(varlist)', factor(1)
matrix list e(L)
esttab e(L) using "C:/Userdata/Shared/Output/Organizations, political participation/factorloadings.tex", replace label


* Panel analysis: Additative index instead of factor for the other participation measures
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period

eststo: xtreg index memberany activeany y2 $covpanel, fe cluster(LopNr) 
estadd local icovs "Yes"
estadd local data "Panel"
estadd local ifix "Yes"
estadd local yfix "Yes"

eststo: xtreg index Nmemberships Nactive y2 $covpanel, fe cluster(LopNr)
estadd local icovs "Yes"
estadd local data "Panel"
estadd local ifix "Yes"
estadd local yfix "Yes"

esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table_panel_index.tex", $textable $stats2 $indextitle $clusternote keep(_cons memberany activeany Nmemberships Nactive)
eststo clear



* Panel analysis: different organizations separately: Voter turnout *
use "dataanalysis_turnout", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period

/*High self-selection*/
eststo clear
foreach k in peace pension { 
	eststo: xtreg turnout_eu member`k' active`k' y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
}
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table_VT_different_high.tex", $textable $stats2 $eptitles $clusternote keep(_cons memberpension activepension memberpeace activepeace) 
eststo clear

/*Low self selection*/
eststo clear
foreach k in boende sport culture {
	eststo: xtreg turnout_eu member`k' active`k' y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
}
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table_VT_different_low.tex", $textable $stats2 $eptitles $clusternote keep(_cons memberboende activeboende membersport activesport memberculture activeculture)
eststo clear

* Panel analysis: different organizations separately: Other participation measures *
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period

/*High self-selection*/
eststo clear
foreach k in environ pension parent woman  { 
	eststo: xtreg factor member`k' active`k' y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
}
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table_OT_different_high.tex", $textable $stats2 $factortitles $clusternote keep(_cons memberpension activepension memberenviron activeenviron memberparent activeparent) 
eststo clear

/*Low self selection*/
eststo clear
foreach k in boende sport handi immig {
	eststo: xtreg factor member`k' active`k' y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
}
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table_OT_different_low.tex", $textable $stats2 $factortitles $clusternote keep(_cons memberboende membersport memberhandi memberimmig activeboende activesport activehandi activeimmig)
eststo clear

 
*All other political participation measures (included in the factor/index) separately as dependent variables *
eststo clear
ds rp_*
foreach k in `r(varlist)' {
	eststo: xtreg `k' memberany activeany y2 $covpanel, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local ifix "Yes"
	estadd local yfix "Yes"
	estadd local data "Panel"
}
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table_othersep.tex", $textable $stats2 $separatetitle $clusternote keep(memberany activeany) 
eststo clear



* Split sample with respect to age *
/* With covariates*/
use "dataanalysis_turnout", clear
eststo: reg turnout_eu memberany activeany $cov if age <= 30, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
eststo: reg turnout_eu Nmemberships Nactive $cov if age <= 30, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
use "dataanalysis_otherparticipation", clear
eststo: reg factor memberany activeany $cov if age <= 30, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
eststo: reg factor Nmemberships Nactive $cov if age <= 30, robust 
	estadd local icovs "Yes"
	estadd local data "Pooled data"
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table2_cov_young.tex", $textable $stats1 $bothtitles $robustnote keep(_cons memberany activeany Nmemberships Nactive)
eststo clear


*Panel analysis: 
use "dataanalysis_turnout", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period
eststo: xtreg turnout_eu memberany activeany y2 $covpanelsplit if age <= 30, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local data "Panel"
eststo: xtreg turnout_eu Nmemberships Nactive y2 $covpanelsplit if age <= 30, fe cluster(LopNr)
	estadd local icovs "Yes"
	estadd local data "Panel"
use "dataanalysis_otherparticipation", clear
destring LopNr period, replace
tabulate period, gen(y)
xtset LopNr period
eststo: xtreg factor memberany activeany y2 $covpanelsplit if age <= 30, fe cluster(LopNr) 
	estadd local icovs "Yes"
	estadd local data "Panel"
eststo: xtreg factor Nmemberships Nactive y2 $covpanelsplit if age <= 30, fe cluster(LopNr)
	estadd local icovs "Yes"
	estadd local data "Panel"
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table3_panel_young.tex", $textable $stats1 $bothtitles $robustnote keep(_cons memberany activeany Nmemberships Nactive)
eststo clear






* Propensity score matching *

* Love plots
forvalues i = 1/3 {
	use "dataanalysis_turnout", clear
	if `i' == 1 teffects psmatch (turnout_eu) (memberany $cov), atet caliper(.2)
	if `i' == 2 teffects psmatch (turnout_eu) (memberany $cov) if activeany == 0, atet caliper(.2)
	if `i' == 3 teffects psmatch (turnout_eu) (activeany $cov) if memberany == 1, atet caliper(.2)
	tebalance density, scheme(lean1) legend(col(2)label(1 "Control")label(2 "Treated")) name(g1)
	graph export "C:/Userdata/Shared/Output/Organizations, political participation/psmatch_density`i'.pdf", as(pdf) replace
	tebalance box, scheme(lean1) legend(col(2)label(1 "Control")label(2 "Treated")) name(g2)
	graph export "C:/Userdata/Shared/Output/Organizations, political participation/psmatch_box`i'.pdf", as(pdf) replace
	gr drop _all
	tebalance summarize
	mat A = r(table)
	mat list A
	svmat2 A, rnames(var)
	gen aa1 = abs(A1)
	gen aa2 = abs(A2)
	replace var = "Years of education" if var == "yearseducation"
	replace var = "Social assistance" if var == "socialbidragfamilj"
	replace var = "Employed" if var == "sysselsatt"
	replace var = "Age" if var == "age"
	replace var = "Gender" if var == "gender"
	replace var = "Immigrant" if var == "immig"
	replace var = "Wage income" if var == "zLoneInk"
	gr dot aa*, over(var) scheme(lean1) legend(label(1 "Raw")label(2 "Matched")position(6)col(2)) ytitle("Absolute standardized difference") linetype(line)lines(lstyle(none)) yline(.1,lpattern(dash)) ylab(0(.1).3)
	graph export "C:/Userdata/Shared/Output/Organizations, political participation/psmatch_loveplot`i'.pdf", as(pdf) replace
}

* Regressions
use "dataanalysis_turnout", clear
eststo: teffects psmatch (turnout_eu) (memberany $cov), atet caliper(.2) 
	estadd local sample "All"
eststo: teffects psmatch (turnout_eu) (memberany $cov) if activeany == 0, atet caliper(.2) 
	estadd local sample "Non-active"
eststo: teffects psmatch (turnout_eu) (activeany $cov) if memberany == 1, atet caliper(.2) 
	estadd local sample "Members"
use "dataanalysis_otherparticipation", clear
eststo: teffects psmatch (factor) (memberany $cov), atet caliper(.2) 
	estadd local sample "All"
eststo: teffects psmatch (factor) (memberany $cov) if activeany == 0, atet caliper(.2) 
	estadd local sample "Non-active"
eststo: teffects psmatch (factor) (activeany $cov) if memberany == 1, atet caliper(.2) 
	estadd local sample "Members"
esttab, se star(* 0.10 ** 0.05 *** 0.01)
esttab using "C:/Userdata/Shared/Output/Organizations, political participation/table_psmatch_t2.tex", $textable $stats3 $bothtitles6c $robustainote keep(*memberany* *activeany*)
filefilter "C:/Userdata/Shared/Output/Organizations, political participation/table_psmatch_t2.tex" "temp.tex", from("r1vs0.") to("") replace
filefilter "temp.tex" "C:/Userdata/Shared/Output/Organizations, political participation/table_psmatch_t2.tex", from("r1vs0.") to("") replace
eststo clear



****
clear all
capture log close


